package com.atguigu.day03;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

//循环栅栏 CyclicBarrier
//集齐七颗龙珠，才能召唤神龙
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(8,()->{
            System.out.println("召唤神龙！！！！！！！！！！！");
        });
        for (int i = 1; i <= 8; i++) {
             new Thread(() -> {
                 try {
                     System.out.println("这是第"+Thread.currentThread().getName()+"颗龙珠");
                     //没集齐之前只能等待
                     cyclicBarrier.await();
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 } catch (BrokenBarrierException e) {
                     throw new RuntimeException(e);
                 }
             }, String.valueOf(i)).start();
        }
    }
}
